package org.delphy.indy2.containers
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	
	import mx.containers.TabNavigator;
	import mx.events.FlexEvent;
	import mx.utils.GraphicsUtil;

	public class CarbonTabNavigator extends TabNavigator
	{
		
		private var _tabBarCornerRadius:Number;

		public function get tabBarCornerRadius():Number
		{ 
			return _tabBarCornerRadius; 
		}

		public function set tabBarCornerRadius(value:Number):void
		{
			if (value !== _tabBarCornerRadius)
			{
				_tabBarCornerRadius = value;
				setStyle("tabBarCornerRadius",value);
				invalidateDisplayList();
			}
		}
		
		private var _tabBarFillColors:Array;

		public function get tabBarFillColors():Array
		{ 
			return _tabBarFillColors; 
		}

		public function set tabBarFillColors(value:Array):void
		{
			if (value !== _tabBarFillColors)
			{
				_tabBarFillColors = value;
				setStyle("tabBarFillColors",value);
			}
		}
				
		public function CarbonTabNavigator()
		{
			super();
			
			styleName="carbonTabNavigator";
			
			addEventListener(FlexEvent.CREATION_COMPLETE,onInit);
		}
		
		private function onInit(evt:FlexEvent):void{
			tabBar.styleName="carbonTabBar";
			tabBar.width=width;
			
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);

			var tabRadius:Number=getStyle("tabBarCornerRadius");
			tabBarFillColors=getStyle("tabBarFillColors");
			
			var aWidth:Number=tabBar.width;
			var aHeight:Number=tabBar.height;
					
			var g:Graphics=tabBar.graphics;
			var mat:Matrix=new Matrix();
			mat.createGradientBox(aWidth,aHeight,Math.PI/2);
			
			g.clear();
			g.beginGradientFill(GradientType.LINEAR,[tabBarFillColors[0],tabBarFillColors[1]],[1,1],[0,255],mat);
			GraphicsUtil.drawRoundRectComplex(g,0,0,aWidth,aHeight,tabRadius,tabRadius,0,0);
			g.endFill();
			
			// Highlight
			mat.createGradientBox(aWidth-4,aHeight/2,Math.PI/2);
			g.beginGradientFill(GradientType.LINEAR,[0xFFFFFF,0xFFFFFF],[0.5,0.1],[0,255],mat);
			g.drawRoundRect(2,0,aWidth-4,aHeight/2,tabRadius,tabRadius);
			g.endFill();		
		}
		
	}
}